<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="schoolName">
        <el-select
          filterable
          v-model="queryParams.schoolName"
          placeholder="名称"
          clearable
        >
          <el-option
            v-for="item in schools"
            :key="item.schoolId"
            :label="item.schoolName"
            :value="item.schoolName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="级别" prop="levelName">
        <el-select
          filterable
          v-model="queryParams.levelId"
          placeholder="级别"
          clearable
        >
          <el-option
            v-for="item in level"
            :key="item.levelId"
            :label="item.levelName"
            :value="item.levelId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          v-preventReClick
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          v-preventReClick
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="编号" align="center" prop="schoolNum" />
      <el-table-column label="logo" align="center" prop="schoolPic">
        <template slot-scope="scope">
          <img
            style="width: 80px; height: auto"
            :src="scope.row.schoolPic"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="schoolName" />
      <el-table-column label="级别" align="center" prop="levelName" />
      <el-table-column label="有效期至" align="center" prop="endTime"
        ><template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime).slice(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime).slice(0, 10) }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 0)"
            >通过</el-button
          >
          <el-button
            v-preventReClick
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 4)"
            >驳回</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form
        @submit.native.prevent
        ref="form"
        :model="form"
        :rules="rules"
        label-width="78px"
      >
        <el-form-item label="名称" prop="schoolName">
          {{ form.schoolName }}
        </el-form-item>
        <el-form-item label="logo" prop="schoolPic">
          <img style="width: 100px" :src="form.schoolPic" alt="" />
        </el-form-item>
        <el-form-item label="级别" prop="levelName">
          {{ form.levelName }}
        </el-form-item>
        <el-form-item label="起止时间">
          {{
            form.startTime
              ? parseTime(form.startTime).slice(0, 10)
              : parseTime(form.startTime)
          }}--{{
            form.endTime
              ? parseTime(form.endTime).slice(0, 10)
              : parseTime(form.endTime)
          }}
        </el-form-item>
        <el-form-item label="凭证" prop="toPayPic" v-if="diatype == 0">
          <el-upload
            class="avatar-uploader"
            :http-request="uploadFileToOSS"
            action="#"
            accept=".jpg,.jpeg,.png"
            :auto-upload="true"
            :show-file-list="false"
            :limit="1"
            list-type="picture"
            ref="upload"
          >
            <img v-if="form.toPayPic" :src="form.toPayPic" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <div style="font-size: 12px; font-weight: 400; color: #a6a6a6">
            支持JPG、PNG格式，图片小于5M
          </div>
        </el-form-item>
        <el-form-item label="驳回原因" prop="rejectReason" v-if="diatype == 4">
          <el-input
          v-input-restrict
            maxlength="100"
            show-word-limit
            :rows="4"
            v-model="form.rejectReason"
            type="textarea"
            placeholder="请输入驳回原因"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-right">
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm">{{
          diatype == 0 ? "通 过" : "驳 回"
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPost_finance,
  getPost_finance,
  updatePost_finance,
} from "@/api/financial/financialAudit";
import { listPost } from "@/api/school/level";
import { listPost_school } from "@/api/school/list";
import { uploadOSS } from "@/api/upload";
export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      activeIndex: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolName: undefined,
        levelId: undefined,
      },
      schools: [],
      level: [],
      diatype: 1,
      imageUrl: "",
      type: [
        { id: 0, name: "缺陷" },
        { id: 1, name: "通过" },
      ],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      // 表单参数
      form: {
        schoolId: null,
        schoolNum: null,
        schoolPic: null,
        schoolName: null,
        levelId: null,
        levelName: null,
        startTime: null,
        endTime: null,
        toPayPic: null,
        rejectReason: null,
        status: null,
      },
      // 表单校验
      rules: {
        rejectReason: [
          { required: true, message: "拒绝原因不能为空", trigger: "blur" },
        ],
        toPayPic: [
          { required: true, message: "支付凭证不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getSchools();
    this.getLevel();
  },
  methods: {
    uploadFileToOSS(file) {
      if (file.file.type.indexOf("image/") == -1) {
        this.$modal.msgError(
          "文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。"
        );
      } else {
        this.$refs.upload.clearFiles();
        uploadOSS(file.file).then((response) => {
          this.form.toPayPic = response[0];
          // this.$forceUpdate()
        });
      }
    },
    getSchools() {
      listPost_school({
        pageNum: 1,
        pageSize: 9999,
        schoolId: this.$store.getters.schoolId,
      }).then((response) => {
        this.schools = response.rows;
      });
    },
    getLevel() {
      listPost({
        pageNum: 1,
        pageSize: 999,
      }).then((response) => {
        this.level = response.rows;
      });
    },
    // el-menu
    handleSelect(key, keyPath) {
      this.queryParams.status = key;
      this.getList();
    },
    // 停用 启用按钮
    handleSwitch(row) {
      this.$confirm(`确定${row.status == 0 ? "停用" : "启用"}吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = Object.assign({}, row);
          data.status = row.status == 0 ? 1 : 0;
          updatePost_finance(data).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        })
        .catch(() => {});
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = 3;
      listPost_finance(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        schoolId: null,
        schoolNum: null,
        schoolPic: null,
        schoolName: null,
        levelId: null,
        levelName: null,
        startTime: null,
        endTime: null,
        toPayPic: null,
        rejectReason: null,
        status: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.levelId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加级别";
    },
    /** 操作按钮 */
    handleUpdate(row, type) {
      this.resetForm("form");
      this.open = true;
      this.title = type == 0 ? "通过审核" : "驳回审核";
      this.diatype = type;
      let data = JSON.parse(JSON.stringify(row));
      data.toPayPic = null;
      this.form = data;
    },
    /** 提交按钮 */
    submitForm: function () {
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.status = this.title == "通过审核" ? 0 : 4;
          data.createTime = this.parseTime(data.createTime);
          data.endTime = this.parseTime(data.endTime);
          data.startTime = this.parseTime(data.startTime);
          updatePost_finance(data).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/financial/export",
        {
          ...this.queryParams,
        },
        `financial_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped></style>
